bitkeeper revision 1.1035 (40e28e04jE3sl2dhBKYQo_m_zSm8BA)
authormjw@wray-m-3.hpl.hp.com <mjw@wray-m-3.hpl.hp.com>
Wed, 30 Jun 2004 09:55:16 +0000 (09:55 +0000)
committermjw@wray-m-3.hpl.hp.com <mjw@wray-m-3.hpl.hp.com>
Wed, 30 Jun 2004 09:55:16 +0000 (09:55 +0000)
Comments and some factoring.

tools/python/xen/xend/Vifctl.py
tools/python/xen/xend/sxp.py

index 49df8f6bd06ca459c7bfc11adc4534839b079153..884ed9677dae9fe8280ba19590fb00a12ce08b18 100644 (file)
@@ -1,3 +1,5 @@
+"""Xend interface to the vifctl script.
+"""
 import os
 import os.path
 import sys
@@ -5,24 +7,30 @@ import sys
 VIFCTL = '/etc/xen/xend/vifctl'
 
 def init():
+    """Call 'vifctl init'. Called when xend starts.
+    """
     os.system(VIFCTL + ' init ')
 
-def up(vif, mac=None, bridge=None, ipaddr=[]):
+def vifctl_args(vif, mac=None, bridge=None, ipaddr=[]):
+    """Construct the argument list for vifctl.
+    """
     args = ['vif=%s' % vif]
     if mac:
         args.append('mac=%s' % mac)
     if bridge:
         args.append('bridge=%s' % bridge)
-    if ipaddr:
-        args.append('ipaddr=%s' % ','.join(ipaddr))
-    os.system(VIFCTL + ' up ' + ' '.join(args))
+    for ip in ipaddr:
+        args.append('ipaddr=%s' % ip)
+    return ' '.join(args)
+    
+def up(vif, **kwds):
+    """Call 'vifctl up' for a vif. Called when a vif is created.
+    """
+    args = vifctl_args(vif, **kwds)
+    os.system(VIFCTL + ' up ' + args)
 
-def down(vif, mac=None, bridge=None, ipaddr=[]):
-    args = ['vif=%s' % vif]
-    if mac:
-        args.append('mac=%s' % mac)
-    if bridge:
-        args.append('bridge=%s' % bridge)
-    if ipaddr:
-        args.append('ipaddr=%s' % ','.join(ipaddr))
-    os.system(VIFCTL + ' down ' + ' '.join(args))
+def down(vif, **kwds):
+    """Call 'vifctl down' for a vif. Called when a vif is destroyed.
+    """
+    args = vifctl_args(vif, **kwds)
+    os.system(VIFCTL + ' down ' + args)
index 01654a23779ea11ecf6d7285b6f91519f656954a..0081f62e3f3f097d709901361a9742a2f45e2f44 100644 (file)
@@ -37,11 +37,14 @@ __all__ = [
     "child2", 
     "child3", 
     "child4", 
-    "child_value", 
+    "child_value",
     "has_id", 
     "with_id", 
     "child_with_id", 
     "elements", 
+    "to_string",
+    "from_string",
+    "all_from_string",
     "parse", 
     ]
 
@@ -339,6 +342,8 @@ class Parser:
             self.state_start(c)
 
 def atomp(sxpr):
+    """Check if an sxpr is an atom.
+    """
     if sxpr.isalnum() or sxpr == '@':
         return 1
     for c in sxpr:
@@ -351,6 +356,8 @@ def atomp(sxpr):
     return 1
     
 def show(sxpr, out=sys.stdout):
+    """Print an sxpr in bracketed (lisp-style) syntax.
+    """
     if isinstance(sxpr, types.ListType):
         out.write(k_list_open)
         i = 0
@@ -366,6 +373,8 @@ def show(sxpr, out=sys.stdout):
         out.write(repr(str(sxpr)))
 
 def show_xml(sxpr, out=sys.stdout):
+    """Print an sxpr in XML syntax.
+    """
     if isinstance(sxpr, types.ListType):
         element = name(sxpr)
         out.write('<%s' % element)
@@ -384,11 +393,24 @@ def show_xml(sxpr, out=sys.stdout):
         out.write(str(sxpr))
 
 def elementp(sxpr, elt=None):
+    """Check if an sxpr is an element of the given type.
+
+    sxpr sxpr
+    elt  element type
+    """
     return (isinstance(sxpr, types.ListType)
             and len(sxpr)
             and (None == elt or sxpr[0] == elt))
 
 def name(sxpr):
+    """Get the element name of an sxpr.
+    If the sxpr is not an element (i.e. it's an atomic value) its name
+    is None.
+
+    sxpr
+
+    returns name (None if not an element).
+    """
     val = None
     if isinstance(sxpr, types.StringType):
         val = sxpr
@@ -397,6 +419,12 @@ def name(sxpr):
     return val
 
 def attributes(sxpr):
+    """Get the attribute list of an sxpr.
+
+    sxpr
+
+    returns attribute list
+    """
     val = []
     if isinstance(sxpr, types.ListType) and len(sxpr) > 1:
         attr = sxpr[1]
@@ -405,6 +433,14 @@ def attributes(sxpr):
     return val
 
 def attribute(sxpr, key, val=None):
+    """Get an attribute of an sxpr.
+
+    sxpr sxpr
+    key  attribute key
+    val  default value (default None)
+
+    returns attribute value
+    """
     for x in attributes(sxpr):
         if x[0] == key:
             val = x[1]
@@ -412,6 +448,13 @@ def attribute(sxpr, key, val=None):
     return val
 
 def children(sxpr, elt=None):
+    """Get children of an sxpr.
+
+    sxpr sxpr
+    elt  optional element type to filter by
+
+    returns children (filtered by elt if specified)
+    """
     val = []
     if isinstance(sxpr, types.ListType) and len(sxpr) > 1:
         i = 1
@@ -426,6 +469,12 @@ def children(sxpr, elt=None):
     return val
 
 def child(sxpr, elt, val=None):
+    """Get the first child of the given element type.
+
+    sxpr sxpr
+    elt  element type
+    val  default value
+    """
     for x in children(sxpr):
         if elementp(x, elt):
             val = x
@@ -433,27 +482,50 @@ def child(sxpr, elt, val=None):
     return val
 
 def child_at(sxpr, index, val=None):
+    """Get the child at the given index (zero-based).
+
+    sxpr  sxpr
+    index index
+    val   default value
+    """
     kids = children(sxpr)
     if len(kids) > index:
         val = kids[index]
     return val
 
 def child0(sxpr, val=None):
+    """Get the zeroth child.
+    """
     return child_at(sxpr, 0, val)
 
 def child1(sxpr, val=None):
+    """Get the first child.
+    """
     return child_at(sxpr, 1, val)
 
 def child2(sxpr, val=None):
+    """Get the second child.
+    """
     return child_at(sxpr, 2, val)
 
 def child3(sxpr, val=None):
+    """Get the third child.
+    """
     return child_at(sxpr, 3, val)
 
 def child4(sxpr, val=None):
+    """Get the fourth child.
+    """
     return child_at(sxpr, 4, val)
 
 def child_value(sxpr, elt, val=None):
+    """Get the value of the first child of the given element type.
+    Assumes the child has an atomic value.
+
+    sxpr sxpr
+    elt  element type
+    val  default value
+    """
     kid = child(sxpr, elt)
     if kid:
         val = child_at(kid, 0, val)
@@ -542,7 +614,21 @@ def from_string(str):
     returns sxpr
     """
     io = StringIO(str)
-    return parse(io)
+    vals = parse(io)
+    if vals is []:
+        return None
+    else:
+        return vals[0]
+    
+
+def all_from_string(str):
+    """Create an sxpr list by parsing a string.
+
+    str string
+    returns sxpr list
+    """    io = StringIO(str)
+    vals = parse(io)
+    return vals
 
 def parse(io):
     """Completely parse all input from 'io'.